package com.emmmya.ocs.modules.system.controller;

import com.emmmya.ocs.common.utils.ResultUtil;
import com.emmmya.ocs.common.vo.Result;
import com.emmmya.ocs.config.redis.ShiroRedisManager;
import com.emmmya.ocs.config.security.shiro.shiroSup.RedisSessionDAO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 创建人：咕噜肉IT小店-黑医
 * 创建时间 2022年10月12日 15:01:22
 * 描述：
 */
@RestController
@RequestMapping("admin")
@Slf4j
@Api(tags={"管理员管理接口"})
public class AdminController {
    @Autowired
    ShiroRedisManager shiroRedisManager;

    @Autowired
    SessionDAO sessionDAO;

    @Autowired
    SessionManager sessionManager;

    @RequestMapping("t/{username}")
    @RequiresRoles("admin")
    @ApiOperation(value="提出某位用户")
    public Result<Object> t(@NonNull @PathVariable("username")String username){
        try {
            RedisSessionDAO redisSessionDAO = (RedisSessionDAO)sessionDAO;
            String sessionId = (String)shiroRedisManager.get(RedisSessionDAO.CURRENT_LOGIN_USER + username);
            shiroRedisManager.del(redisSessionDAO.getRedisSessionKey(sessionId));
            shiroRedisManager.del(RedisSessionDAO.CURRENT_LOGIN_USER+username);
        } catch (Exception e) {
            // e.printStackTrace();
            log.warn("踢出【"+username+"】失败，错误信息："+e.getMessage());
            return ResultUtil.error(e);
        }
        return ResultUtil.success("踢出【"+username+"】成功！");

    }
}
